package com.study.samsung.Control.htControl;

import cn.hutool.core.io.FileUtil;
import com.study.samsung.Entity.Classification;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Service.ClassificationService;
import com.study.samsung.Service.CommodityService;
import com.study.samsung.Service.SuccessionService;
import com.study.samsung.Utils.BaseServlet;
import sun.text.resources.FormatData;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/goodsadd")
@MultipartConfig
public class GoodsAddServlet extends BaseServlet {
    SuccessionService ss = new SuccessionService();
    ClassificationService cs = new ClassificationService();
    CommodityService cos = new CommodityService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object obj = session.getAttribute("admin");
        if (obj==null){
            return "f:htjsp/login";
        }else {
            request.setCharacterEncoding("utf-8");
            List<Succession> successions = ss.selectAll();
            session.setAttribute("successions",successions);
            List<Classification> classifications = cs.selectAll();
            session.setAttribute("classifications",classifications);
            return"f:htjsp/goodsadd";
        }

    }

    public Integer addgoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        String oldName;
        String stoPath;
        String outPath;
        String newName;

        String sname = request.getParameter("level-name");
        String Sprice = request.getParameter("level-price");
        String Lidone = request.getParameter("level-leibieone");
        String Lidtwo = request.getParameter("level-leibietwo");
        String Isdefault = request.getParameter("level-zhuangtai");
        Part part = request.getPart("myfile");
        BigDecimal sprice =new BigDecimal(Sprice);
        Integer lidone = Integer.parseInt(Lidone);
        Integer lidtwo = Integer.parseInt(Lidtwo);
        boolean isdefault = Boolean.parseBoolean(Isdefault);

        String myfile=null;
        oldName = part.getSubmittedFileName();//获取文件的原始名字
        if (part.getSize()>0){
            String realPath = request.getServletContext().getRealPath("/");
            //D:\Java03\YF03405\02Java\code\Java\Git\meizu\out\artifacts\meizu_Web_exploded\
            stoPath=realPath.substring(0,realPath.indexOf("out"))+"web/img/"+sname;
            outPath=realPath+"img/"+sname;
            newName =getSuffix(oldName);
            //上传头像文件
            FileUtil.writeFromStream(part.getInputStream(),stoPath+newName);
            myfile = "../img/"+sname+newName;
        }

        Commodity commodity=new Commodity (null,sname,myfile,sprice,isdefault,lidone);
        Integer update = cos.addgoods(commodity);
        return update;
    }

    public String getSuffix(String oldName){
        //.png/.jpg……
        int i = oldName.lastIndexOf(".");
        return oldName.substring(i);
    }
    //生成唯一的文件名
    public String uniqeName(){
        //获取当前系统时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String s=sdf.format(date);
        //生成四位数的随机数  1-9999之间，不够四位的，补齐四位 0001  0025
        int rnd = (int)(Math.random()*9999)+1;
        //rnd=4
        String n = rnd+"";
        for(int i=0;i<(4-n.length());i++){
            n="0"+n;
        }
        return s+n;
    }
}
